package vg.modules.backlight;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;

/**
 * This plugin finds cycle in current subgraph.
 * @author tzolotuhin
 */
public class CycleSearcher {
	private JButton button;
	private AGraphView view;

	// -------------------------------------------------------------------------
	public CycleSearcher() {
		// ---------------------------------------
		this.button = new JButton(new ImageIcon("./data/resources/textures/circle.png"));
		this.button.setToolTipText("Find all cycles, which have current vertex");
		this.button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (CycleSearcher.this.view == null)
					return;
				Graph graph = view.getGraphInformation().getSelectionSubGraph();
				if (graph != null) {
					Collection<Vertex> lv = graph.getVertices();
					if (lv != null && lv.size() == 1) {
						Vertex sv = lv.iterator().next();
						view.getGraphAlgorithms().showCycles(sv);
					}
				}

			}
		});
		this.button.setEnabled(false);
	}

	// -------------------------------------------------------------------------
	public JComponent getView() {
		return (this.button);
	}
	
	public void setEnabled(boolean enable) {
		this.button.setEnabled(enable);
	}
	public void changeView(AGraphView igv) {		
		this.view = igv;
		if (view == null) {
			setEnabled(false);
		}
	}
}